//
// Created by francklinson on 2021/10/23 AT 16:23.
//
#include <iostream>
#include <string>
#include <fstream>
#include <optional>

std::optional<std::string> ReadFileAsString(const std::string &filepath) {
    std::ifstream stream(filepath);
    if (stream) {
        // read file
        std::string result;
        stream.close();
        return result;
    }
    return {};
}

int main() {
    std::optional<std::string> data = ReadFileAsString("./data.txt"); // optional 增加变量的状态
    std::string value = data.value_or("Not present!");
    std::cout << value << std::endl;
    if (data.has_value()) {
        std::cout << "File read successfully!" << std::endl;
    } else {
        std::cout << "File can not be read!" << std::endl;
    }
    return 0;
}

